/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlet;

import java.io.IOException;

import javax.mail.Address;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Rodrigo
 */
@WebServlet(name = "EnviodeMensaje", urlPatterns = {"/EnviodeMensaje"})
public class EnviodeMensaje extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            String nombres = request.getParameter("FirstName");
            String apellidos = request.getParameter("LastName");
            String email = request.getParameter("Email");
            String comentario = request.getParameter("Coment");
            String correo = "grupoamate2013@gmail.com";
            
            Properties props = new Properties();
            props.setProperty("mail.smtp.host", "smtp.gmail.com");
            props.setProperty("mail.smtp.starttls.enable", "true");
            props.setProperty("mail.smtp.port", "587");
            props.setProperty("mail.smtp.user", correo);
            props.setProperty("mail.smtp.auth", "true");
            Session session = Session.getDefaultInstance(props);
            session.setDebug(true);
            MimeMessage message = new MimeMessage(session);
            message.setSubject("Contacto: " + nombres + apellidos + email);
            String html =  "<table style='border-bottom: 1px dashed rgb(237, 237, 237);' align='center' border='0' width='100%' cellpadding='0' cellspacing='0'>\n" +
"    <tr>\n" +
"        <td>\n" +
"            <table align='center' border='0' width='600' cellpadding='0' cellspacing='0'>\n" +
"                <tr>\n" +
"                    <td style='padding: 0pt 20px;' height='40'>\n" +
"                        <p style='font-size: 12px; line-height: 16px; font-family: Helvetica,Arial,sans-serif; color: rgb(153, 153, 153);'>\n" +
"                        </p>\n" +
"                    </td>\n" +
"                </tr>\n" +
"            </table>\n" +
"        </td>\n" +
"    </tr>\n" +
"</table>\n" +
"<table align='center' border='0' width='600' cellpadding='0' cellspacing='0'>\n" +
"    <tr>\n" +
"        <td\n" +
"            style='padding-right: 20px; padding-left: 20px;'>\n" +
"            <table align='center' border='0' width='560' cellpadding='0' cellspacing='0'>\n" +
"                <tr>\n" +
"                    <td align='left' width='450'>\n" +
"                        <h1 style='font-size: 20px; font-weight: bold; font-family: Helvetica,Arial,sans-serif; line-height: 28px; color: rgb(153, 153, 153);'>\n" +
"                            &#161;Hola hemos recibido un correo de una cliente!</h1>\n" +
"                    </td>\n" +
"                    \n" +
"                   \n" +
"                </tr>\n" +
"            </table>\n" +
"        </td>\n" +
"    </tr>\n" +
"    <tr>\n" +
"        <td style='padding-right: 20px; padding-left: 20px;'>\n" +
"            <h2 style='font-weight: bold; font-size: 18px; font-family: Helvetica,Arial,sans-serif; line-height: 26px; color: rgb(153, 153, 153);'></h2>\n" +
"        </td>\n" +
"    </tr>\n" +
"    <tr>\n" +
"        <td style='padding: 20px;'>\n" +
"            <div style='font: 22px/19px Helvetica,Arial,sans-serif; color: black(51, 51, 51);'><br />" + comentario + "</div></td></tr>\n" +
"</table>\n" +
"<table align='center' border='0' width='600' cellpadding='0' cellspacing='0'>\n" +
"    <tr>\n" +
"        <td style='padding: 30px 20px 20px;'>\n" +
"            <p style='font-weight:  bold; font-size: 18px; line-height: 24px; font-family: Helvetica,Arial,sans-serif; color: blue;'><br>Atte: Grupo Amate</p>\n" +
"        \n" +
"        </td>\n" +
"    <tr>\n" +
"         <td align='left' width='110'>\n" +
"                        <a href='' style='line-height: 0;' target='_blank'><img src='http://img707.imageshack.us/img707/8229/os8r.jpg' alt='' border='1' vspace='20'>\n" +
"                        </a>\n" +
"                    </td>\n" +
"   \n" +
"    </tr>\n" +
"</table>";
            message.setContent(html, "text/html; charset=utf-8");
            Address address = new InternetAddress(correo);
            message.setFrom(address);
            address = new InternetAddress(correo, false);
            message.addRecipient(Message.RecipientType.TO, address);
            Transport t = session.getTransport("smtp");
            t.connect(correo, "grupoamate#2013");
            t.sendMessage(message, message.getAllRecipients());
        } catch (MessagingException ex) {
            Logger.getLogger(EnviodeMensaje.class.getName()).log(Level.SEVERE, null, ex);
        }
        response.sendRedirect("index.jsp");
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
